package com.live2d.service;

import com.live2d.entity.json.*;
import com.zwh.tool.utils.FileUtils;
import com.zwh.tool.utils.JsonUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 伊甸园的骄傲live2d
 */
public class ydyLive2dService {
    //移动文件路径
    private static String MOVE_FILE_PATH = "D:\\软件\\逆向\\ces\\ydy";
    //游戏名称
    private static String GAME_NAME = "伊甸园的骄傲";

    public static void main(String[] args) {
//        File file = new File("D:\\软件\\逆向\\ydy\\ccc");
//        moveFile(file);

//        File file = new File("D:\\软件\\逆向\\ydy\\live2d");
//        arrangeFile(file);

        File file = new File(MOVE_FILE_PATH);
        batFile(file);
    }


    /**a
     * 批处理文件,创建配置文件
     * @return
     */
    private static void batFile(File file){
        if (file.isDirectory()) {
            if (judgeFile(file)){
                ConfigJson configJson = bulidConfigJson(file);
                FileUtils.saveFile(configJson,file.getParentFile().getParent()+"\\" +file.getName() +".json");
            }
            File[] tmps = file.listFiles();
            for (File tmp : tmps) {
                System.out.println(tmp.getName() + ":tmps");
                batFile(tmp);
            }
        }
    }
    /**
     * 判断目录下是否有skel、atlas、png文件
     * @param file
     * @return
     */
    private static boolean judgeFile(File file){
        File[] tmps = file.listFiles();
        boolean skel = false;
        boolean atlas = false;
        boolean png = false;
        for (File tmp : tmps) {
            if (tmp.getAbsolutePath().endsWith(".skel")){
                skel = true;
            }
            if (tmp.getAbsolutePath().endsWith(".atlas")){
                atlas = true;
            }
            if (tmp.getAbsolutePath().endsWith(".png")){
                png = true;
            }
        }
        return skel && atlas && png;
    }
    /**
     * 构造配置文件
     * @param file 是个目录，目录下得有skel、atlas、png文件
     * @return
     */
    private static ConfigJson bulidConfigJson(File file){
        String fileName = file.getName();
        String parenName = file.getParentFile().getName();
        ConfigJson configJson = new ConfigJson();
        configJson.setType(9);

        Controllers controllers = new Controllers();
        controllers.setIntimacy_system(new Intimacy_system());
        controllers.setSlot_opacity(new Slot_opacity());
        controllers.setSlot_color(new Slot_color());
        configJson.setControllers(controllers);

        List<Hit_areas> hit_areas = new ArrayList<>();
        Hit_areas hit_areas1 = new Hit_areas();
        hit_areas1.setName("root");
        hit_areas1.setId("root");
        hit_areas1.setWidth(1.5);
        hit_areas1.setHeight(1.5);
        hit_areas.add(hit_areas1);
        configJson.setHit_areas(hit_areas);

        Map<String,List<Idle>> motions = new HashMap<>();
        motions.put("idle",bulidIdles("idle"));
//        motions.put("",bulidIdles("Stand"));
        configJson.setMotions(motions);

        Options options = new Options();
        options.setScale_factor(0.15);
        options.setEdge_padding(false);
        options.setShader("Skeleton-Straight-Alpha");
        configJson.setOptions(options);

        List<Atlases> atlases = new ArrayList<>();
        Atlases atlases1 = new Atlases();
        List<String> texNames = new ArrayList<>();
        List<String> textures = new ArrayList<>();
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.getName().endsWith(".skel")){
                configJson.setSkeleton(parenName + "/" +fileName + "/" +f.getName());
            }
            if (f.getName().endsWith(".atlas")){
                atlases1.setAtlas(parenName + "/" +fileName + "/" + f.getName());
            }
            if (f.getName().endsWith(".png")){
                texNames.add(f.getName().substring(0,f.getName().length()-4));
                textures.add(parenName + "/" +fileName + "/" + f.getName());
            }
        }
        atlases1.setTextures(textures);
        atlases1.setTex_names(texNames);
        atlases.add(atlases1);
        configJson.setAtlases(atlases);
        System.out.println(JsonUtil.toJson(configJson));
        return configJson;
    }

    private static List<Idle> bulidIdles(String stand) {
        List<Idle> idles = new ArrayList<>();
        Idle idle = new Idle();
        idle.setFile(stand);
        idles.add(idle);
        return idles;
    }
    /**
     *整理文件,把相同角色的服装放到一个文件夹里
     */
    private static  void arrangeFile(File file){
        //获取所有不是目录的文件
        List<File> files = new ArrayList<>();
        FileUtils.list(files,file);
        //先整理
        files.forEach(item ->{
            System.out.println(item.getName());
            Pattern compile = Pattern.compile("[A-z]*_");
            Matcher m = compile.matcher(item.getName());
            if (m.find()) {
                String group = m.group(0);
                String name = item.getName();
                String lowerName = item.getName().toLowerCase();
                if (lowerName.contains("_bg") ){
                    name = name.substring(0, lowerName.indexOf("_bg"));
                }if (name.contains(".atlas") ){
                    name = name.substring(0, name.indexOf(".atlas"));
                }if (name.contains(".png") ){
                    name = name.substring(0, name.indexOf(".png"));
                }if (name.contains(".skel") ){
                    name = name.substring(0, name.indexOf(".skel"));
                }
                //移动文件
                FileUtils.moveFile(item,MOVE_FILE_PATH + "\\" + group.substring(0,group.length()-1) +"\\" + name );
            } else {
                System.out.println("NO MATCH");
            }
        });

    }



    /**
     *整理文件,把skel、atlas、png文件先拉到一个文件夹里
     */
    private static void moveFile(File file){
        //获取所有不是目录的文件
        List<File> files = new ArrayList<>();
        FileUtils.list(files,file);
        files.forEach(item ->{
            if (item.getName().contains("ui_yiji_boss")
                    || item.getName().contains("CardShowSpine")
                    || item.getName().contains("ui_tushuguan")
                    || item.getName().contains("ui_tushuguan")
                    || item.getName().contains("ui_tushuguan")){
                FileUtils.moveFile(item,"D:\\软件\\逆向\\ydy\\live2d");
            }
        });
    }
}
